= camunda BPMN model API

The camunda BPMN model API is a simple, lightweight Java library for parsing, creating and editing BPMN 2.0 XML files. It is licensed under the Apache 2.0 license.

[NOTE]
====
camunda BPMN model can be used standalone, independenly of Camunda Platform / process engine.
====

* Web Site: https://camunda.com/
* User Guide: link:http://docs.camunda.org/latest/guides/user-guide/#bpmn-model-api[http://docs.camunda.org]
* Examples: link:https://github.com/camunda/camunda-bpm-examples/tree/master/bpmn-model-api[https://github.com/camunda/camunda-bpm-examples]
* Issue Tracker: https://app.camunda.com/jira/
* Contribution Guidelines: https://camunda.com/learn/community/#contribute

== Features

=== Fluent builder API

To easily create simple BPMN 2.0 processes we provide the fluent builder API. It supports most BPMN 2.0
elements and allows you to set attributes and add some child elements. The following process is deployable
and can be executed by the Camunda Platform.

[source,java]
----
BpmnModelInstance modelInstance = Bpmn.createExecutableProcess("processId")
  .startEvent()
  .userTask()
  .endEvent()
  .done();
----

For more complex examples see the link:https://docs.camunda.org/manual/latest/user-guide/model-api/bpmn-model-api/fluent-builder-api/[docs],
the tests in link:src/test/java/org/camunda/bpm/model/bpmn/builder/ProcessBuilderTest.java[ProcessBuilderTest.java] or this
link:https://github.com/camunda/camunda-bpm-examples/tree/master/bpmn-model-api/generate-process-fluent-api[example].

=== Create an empty BPMN model

You can easily create a new empty BPMN model.

[source,java]
----
BpmnModelInstance modelInstance = Bpmn.createEmptyModel();
Definitions definitions = modelInstance.newInstance(Definitions.class);
definitions.setTargetNamespace("http://camunda.org/examples");
modelInstance.setDefinitions(definitions);
----

=== Validate a BPMN model

At any time you can validate your model against the BPMN 2.0 specification.

[source,java]
----
BpmnModelInstance modelInstance = [...]
Bpmn.validateModel(modelInstance);
----

=== Creating a Process using the full API

The fluent API is usually a good choice for creating a new BPMN process.
However, not all elements supported by the model are exposed through fluent
builders and the fluent api may not be the appropriate API style for all usecases.
In such cases, the full API can be used for creating a process model.
The following code creates a simple Process as a jUnit4 test using the full API:
link:src/test/java/org/camunda/bpm/model/bpmn/CreateModelTest.java[CreateModelTest.java].

=== Find elements inside the model

[source,java]
----
// find element instance by ID
ModelElementInstance elementInstance = modelInstance.getModelElementById("start");

// find all elements of the type task
ModelElementType taskType = modelInstance.getModel().getType(Task.class);
Collection<ModelElementInstance> elementInstances = modelInstance.getModelElementsByType(taskType);
----

=== Save the BPMN model

The model API allows you to save your model in several ways

[source,java]
----
// create BPMN model
BpmnModelInstance modelInstance = [...]

// convert to string
String xmlString = Bpmn.convertToString(modelInstance);

// write to output stream
OutputStream outputStream = new OutputStream(...);
Bpmn.writeModelToStream(outputStream, modelInstance);

// write to file
File file = new File(...);
Bpmn.writeModelToFile(file, modelInstance);

----

== Planned features

* extended validation support like integrity checks for broken references


== FAQ

=== What is BPMN 2.0?

Read the https://camunda.com/bpmn/[BPMN 2.0 Tutorial on camunda.com].

=== Which Java (JRE) Version is required?

Java JRE 1.8+ is required. We test camunda BPMN model API on Oracle and IBM JVMs.

=== Does this API support all BPMN 2.0 Elements?

No, but support is already quite extensive. See
link:src/main/java/org/camunda/bpm/model/bpmn/instance[org.camunda.bpm.model.bpmn.instance]
for a list of currently supported elements.
